package com.h3c.ptability.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.h3c.ptability.cache.PtEnumCache;
import com.h3c.ptability.dto.PtDeptDTO;
import com.h3c.ptability.entity.PtDept;
import com.h3c.ptability.mapper.PtDeptMapper;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Component
public class DeptTree implements InitializingBean {

    @Resource
    PtDeptMapper ptDeptMapper;

    private PtDeptDTO root = new PtDeptDTO();

    @Override
    public void afterPropertiesSet() throws Exception {
        LambdaQueryWrapper<PtDept> queryWrapper = new QueryWrapper<PtDept>().lambda();
        queryWrapper.eq(PtDept::getDeptId, BusiEnumDefine.H3C_CODE)
                .eq(PtDept::getDeptStatus, 1);
        PtDept rootDept  = ptDeptMapper.selectOne(queryWrapper);
        if (rootDept != null){
            BeanUtils.copyProperties(rootDept, this.root);
            getDeptTree(root);
        }
    }

    public void getDeptTree(PtDeptDTO root){
        LambdaQueryWrapper<PtDept> queryWrapper = new QueryWrapper<PtDept>().lambda();
        queryWrapper.eq(PtDept::getParentId, root.getDeptId())
                .eq(PtDept::getDeptStatus, 1);
        List<PtDept> childrenDept = ptDeptMapper.selectList(queryWrapper);
        if (childrenDept.size() > 0) {
            List<PtDeptDTO> childList = new ArrayList<>();
            for (PtDept ptDept : childrenDept) {
                PtDeptDTO childNode = new PtDeptDTO();
                BeanUtils.copyProperties(ptDept, childNode);
                childNode.setParent(root);
                childList.add(childNode);
            }
            root.setChildren(childList);
            root.setHasChildren(true);
            for (PtDeptDTO deptTreeNode : childList) {
                getDeptTree(deptTreeNode);
            }
        } else {
            root.setHasChildren(false);
        }
    }

    public PtDeptDTO getChildTree(Integer deptId){
        PtDeptDTO pointer = null;
        try {
            getChildTree(deptId, null, pointer);
        } catch (ResultException e){
            pointer =(PtDeptDTO)e.getExceptionRes();
        }
        return pointer;
    }

    public void getChildTree(Integer deptId, PtDeptDTO rootNode, PtDeptDTO pointer){
        if (pointer != null){
            return;
        }
        if (rootNode == null) {
            rootNode = this.root;
        }
        if (rootNode.getDeptId().equals(deptId)){
            pointer =  SerializationUtils.clone(rootNode);
            pointer.setParent(null);
            throw new ResultException(pointer);
        } else {
            if (!CollectionUtils.isEmpty(rootNode.getChildren())){
                for (PtDeptDTO child : rootNode.getChildren()) {
                    getChildTree(deptId, child, pointer);
                }
            }
        }

    }

}
